package com.mybatis.firstMybatis;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.yiibai.mybatis.models.User;
import com.yiibai.mybatis.models.UserMapper;

/**
 * Hello world!
 *
 */
public class App {
	private static SqlSessionFactory sqlSessionFactory;
	private static Reader reader;

	static {
		String resource = "config/mybatis-config.xml";
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
//		try {
//			reader = Resources.getResourceAsReader("config/mybatis-config.xml");
//			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	public static SqlSessionFactory getSession() {
		return sqlSessionFactory;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SqlSession session = sqlSessionFactory.openSession();
		try {
			//User user1 = (User) session.selectOne("com.yiibai.mybatis.models.UserMapper.GetUserByID", 1);
			
			UserMapper mapper = session.getMapper(UserMapper.class);
			User user = mapper.selectUser(1);

			if (user != null) {
				String userInfo = "名字：" + user.getName() + ", 所属部门：" + user.getDept() + ", 主页：" + user.getWebsite();
				System.out.println(userInfo);
			}
		} finally {
			session.close();
		}
	}
}
